package 计时器;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MillisTime extends JFrame implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                long start=System.currentTimeMillis();
                long s=0, ms;
                while (true){
                    ms=System.currentTimeMillis()-start;
                    s=ms/1000;ms=ms%1000;
                    String ss=s+"",sms=ms+"";
                    if (s<10)ss="0"+ss;
                    if (ms<10)sms="00"+ms;
                    else if(ms<100) sms="0"+ms;
                    String str=ss+":"+sms;
                    label.setText(str);
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
    private JButton btn ;
    private JLabel label;
    public MillisTime(){
        setTitle("秒表");
        setSize(300,300);
        setVisible(true);
        setLayout(null);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        label =new JLabel("00:000");
        label.setBounds(50,50,200,60);
        add(label);

        btn=new JButton("开始");
        btn.setBounds(20,200,60,25);
        btn.addActionListener(this);
        add(btn);

        Font font=new Font("微软雅黑",Font.BOLD,36);
        label.setFont(font);
    }
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MillisTime();
            }
        });
    }
}
